<template>
  <el-main>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/device' }"
        >设备管理
      </el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/device/unbind' }"
        >库存设备
      </el-breadcrumb-item>
      <el-breadcrumb-item>修改设备信息</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="margin-top-20">
      <el-card shadow="never">
        <el-form :model="form" :rules="rules" label-width="120px" ref="form">
          <el-form-item label="设备类型" prop="deviceType">
            <el-radio-group v-model="form.deviceType">
              <el-radio
                :key="item.code"
                :label="item.code"
                v-for="item in device_type"
                >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="通信方式" prop="deviceSocket">
            <el-radio-group v-model="form.deviceSocket">
              <el-radio
                :key="item.code"
                :label="item.code"
                v-for="item in device_socket"
                >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item class="m-input-400" label="imei" prop="imei">
            <el-input autocomplete="off" v-model="form.imei" />
          </el-form-item>
          <el-form-item class="m-input-400" label="设备波段" prop="waveLength">
            <el-input autocomplete="off" v-model="form.waveLength">
              <template slot="append"
                >MHz
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="平台信息" prop="platformType">
            <el-radio-group v-model="form.platformType">
              <el-radio
                :key="item.code"
                :label="item.code"
                v-for="item in platform_type"
                >{{ item.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item class="m-input-400" label="备注" prop="remarks">
            <el-input type="textarea" v-model="form.remarks"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="submitForm('form')" type="primary"
              >立即注册
            </el-button>
            <el-button @click="cancelForm">取消</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </el-main>
</template>

<script>
export default {
  data() {
    return {
      id: null,
      form: {
        deviceType: null,
        deviceSocket: null,
        imei: null,
        waveLength: null,
        platformType: null,
        remarks: null
      },
      rules: {
        deviceType: [
          { required: true, message: "请选择设备类型", trigger: "none" }
        ],
        deviceSocket: [
          { required: true, message: "请选择设备类型", trigger: "none" }
        ],
        platformType: [
          { required: true, message: "请选择设备类型", trigger: "none" }
        ],
        imei: [{ required: true, message: "请输入imei", trigger: "none" }],
        waveLength: [{ required: true, message: "请输入波段", trigger: "none" }]
      },
      device_type: [],
      device_socket: [],
      platform_type: []
    };
  },
  mounted() {
    this.id = this.$route.params.id;
    this.info();
    this.load_device_type();
    this.load_device_socket();
    this.load_platform_type();
  },
  methods: {
    info() {
      this.$ajax.device_unbind
        .info(this.id)
        .then(result => {
          if (result.code == 1) {
            this.form = result.data;
          } else {
            this.$message({
              message: result.msg,
              type: "warning"
            });
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let loadingInstance = this.$loading({ fullscreen: true });
          this.$ajax.device_unbind
            .update(this.form)
            .then(result => {
              if (result.code != 1) {
                this.$message({
                  message: result.msg,
                  type: "warning"
                });
              } else {
                this.$message({
                  type: "success",
                  message: "修改设备信息成功!"
                });
                this.$router.push("/device/unbind/");
              }
            })
            .catch(e => {
              console.log(e);
              this.$message({
                message: "系统繁忙，请稍后重试",
                type: "warning"
              });
            })
            .finally(function() {
              loadingInstance.close();
            });
        } else {
          return false;
        }
      });
    },
    cancelForm() {
      this.$router.push("/device/unbind");
    },
    load_device_type() {
      this.$ajax.support
        .device_type()
        .then(result => {
          if (result.code === 1) {
            this.device_type = result.data;
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
    load_device_socket() {
      this.$ajax.support
        .device_socket()
        .then(result => {
          if (result.code === 1) {
            this.device_socket = result.data;
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
    load_platform_type() {
      this.$ajax.support
        .platform_type()
        .then(result => {
          if (result.code === 1) {
            this.platform_type = result.data;
          }
        })
        .catch(e => {
          console.log(e);
        });
    }
  }
};
</script>
